#include "wxwidgetsguiconfigpanel.h"
#include "../wxsproject.h"
#include <wx/tokenzr.h>
#include <wx/filedlg.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <prep.h>

const long wxWidgetsGUIConfigPanel::ID_TEXTCTRL1 = wxNewId();
const long wxWidgetsGUIConfigPanel::ID_BUTTON1 = wxNewId();
const long wxWidgetsGUIConfigPanel::ID_BUTTON2 = wxNewId();
const long wxWidgetsGUIConfigPanel::ID_COMBOBOX1 = wxNewId();
const long wxWidgetsGUIConfigPanel::ID_CHECKBOX2 = wxNewId();
const long wxWidgetsGUIConfigPanel::ID_CHECKBOX1 = wxNewId();

BEGIN_EVENT_TABLE( wxWidgetsGUIConfigPanel, cbConfigurationPanel )
END_EVENT_TABLE()

wxWidgetsGUIConfigPanel::wxWidgetsGUIConfigPanel( wxWindow* parent, wxWidgetsGUI* GUI ): m_GUI( GUI ) {
  static const wxWindowID id = -1;
  wxBoxSizer* BoxSizer4;
  wxStaticBoxSizer* StaticBoxSizer2;
  wxStaticText* StaticText2;
  wxButton* Button1;
  wxStaticText* StaticText1;
  wxBoxSizer* BoxSizer2;
  wxButton* Button2;
  wxStaticBoxSizer* StaticBoxSizer3;
  wxBoxSizer* BoxSizer1;
  wxStaticBoxSizer* StaticBoxSizer1;
  wxBoxSizer* BoxSizer3;
  Create( parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T( "id" ) );
  BoxSizer1 = new wxBoxSizer( wxVERTICAL );
  BoxSizer2 = new wxBoxSizer( wxHORIZONTAL );
  StaticBoxSizer1 = new wxStaticBoxSizer( wxVERTICAL, this, _T( "自动加载的资源:" ) );
  StaticText1 = new wxStaticText( this, wxID_ANY, _T( "每行将作为文件名传递给\nwxXmlResource::Get()->Load." ), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE, _T( "wxID_ANY" ) );
  StaticBoxSizer1->Add( StaticText1, 0, wxEXPAND, 5 );
  AutoLoad = new wxTextCtrl( this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize( 218, 102 ), wxTE_MULTILINE, wxDefaultValidator, _T( "ID_TEXTCTRL1" ) );
  StaticBoxSizer1->Add( AutoLoad, 1, wxTOP | wxEXPAND, 5 );
  BoxSizer3 = new wxBoxSizer( wxHORIZONTAL );
  Button1 = new wxButton( this, ID_BUTTON1, _T( "添加文件" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON1" ) );
  BoxSizer3->Add( Button1, 0, wxEXPAND, 5 );
  BoxSizer3->Add( -1, -1, 1, wxALIGN_CENTER_VERTICAL, 5 );
  Button2 = new wxButton( this, ID_BUTTON2, _T( "清空" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON2" ) );
  BoxSizer3->Add( Button2, 0, wxALIGN_CENTER_VERTICAL, 5 );
  StaticBoxSizer1->Add( BoxSizer3, 0, wxTOP | wxEXPAND, 5 );
  BoxSizer2->Add( StaticBoxSizer1, 1, wxEXPAND, 5 );
  BoxSizer4 = new wxBoxSizer( wxVERTICAL );
  StaticBoxSizer2 = new wxStaticBoxSizer( wxVERTICAL, this, _T( "Main resource" ) );
  MainRes = new wxComboBox( this, ID_COMBOBOX1, wxEmptyString, wxDefaultPosition, wxSize( 201, 21 ), 0, 0, wxCB_READONLY, wxDefaultValidator, _T( "ID_COMBOBOX1" ) );
  StaticBoxSizer2->Add( MainRes, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
  StaticText2 = new wxStaticText( this, wxID_ANY, _T( "初始化应用程序时 将创建并显示在此选择的窗口\n你应该在这里用一个框架.\n使用对话框作为主资源也是可以接受的 但可能导致关闭主对话框后终止应用程序的问题." ),
                                  wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE, _T( "wxID_ANY" ) );
  StaticBoxSizer2->Add( StaticText2, 0, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5 );
  BoxSizer4->Add( StaticBoxSizer2, 1, wxLEFT | wxEXPAND, 5 );
  StaticBoxSizer3 = new wxStaticBoxSizer( wxVERTICAL, this, _T( "XRC 系统初始化" ) );
  InitAll = new wxCheckBox( this, ID_CHECKBOX2, _T( "Call wxXmlResource::InitAllHandlers()" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_CHECKBOX2" ) );
  InitAll->SetValue( false );
  StaticBoxSizer3->Add( InitAll, 0, wxEXPAND, 5 );
  InitAllNecessary = new wxCheckBox( this, ID_CHECKBOX1, _T( "只有在必要时" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_CHECKBOX1" ) );
  InitAllNecessary->SetValue( false );
  InitAllNecessary->Disable();
  StaticBoxSizer3->Add( InitAllNecessary, 0, wxTOP | wxEXPAND, 5 );
  BoxSizer4->Add( StaticBoxSizer3, 0, wxTOP | wxLEFT | wxEXPAND, 5 );
  BoxSizer2->Add( BoxSizer4, 0, wxEXPAND, 5 );
  BoxSizer1->Add( BoxSizer2, 1, wxALL | wxEXPAND, 5 );
  SetSizer( BoxSizer1 );
  BoxSizer1->Fit( this );
  BoxSizer1->SetSizeHints( this );
  Connect( ID_BUTTON1, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxWidgetsGUIConfigPanel::OnButton1Click ) );
  Connect( ID_BUTTON2, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxWidgetsGUIConfigPanel::OnButton2Click ) );
  Connect( ID_CHECKBOX2, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( wxWidgetsGUIConfigPanel::OnInitAllChange ) );
  for( size_t i = 0; i < m_GUI->m_LoadedResources.Count(); ++i ) {
    AutoLoad->AppendText( m_GUI->m_LoadedResources[i] );
    AutoLoad->AppendText( _T( "\n" ) );
  }
  MainRes->Append( _T( "-- None --" ) );
  wxArrayString Resources;
  m_GUI->EnumerateMainResources( Resources );
  MainRes->Append( Resources );
  if( m_GUI->m_MainResource.empty() ) {
    MainRes->SetValue( _T( "-- None --" ) );
  } else {
    MainRes->SetValue( m_GUI->m_MainResource );
  }
  InitAll->SetValue( m_GUI->m_CallInitAll );
  InitAllNecessary->Enable( m_GUI->m_CallInitAll );
  InitAllNecessary->SetValue( m_GUI->m_CallInitAllNecessary );
}

wxWidgetsGUIConfigPanel::~wxWidgetsGUIConfigPanel() {}

void wxWidgetsGUIConfigPanel::OnInitAllChange( M_Code_unused wxCommandEvent & event ) {
  InitAllNecessary->Enable( InitAll->GetValue() );
}

void wxWidgetsGUIConfigPanel::OnButton1Click( M_Code_unused wxCommandEvent & event ) {
  wxString FileName = ::wxFileSelector( _T( "选择资源文件" ), _T( "" ), _T( "" ), _T( "" ),
                                        _T( "XRC文件 (*.xrc)|*.xrc| 压缩包 (*.zip)|*.zip| All files (*)|*" ),
                                        wxFD_OPEN | wxFD_FILE_MUST_EXIST | compatibility::wxHideReadonly );
  if( !FileName.empty() ) {
    wxFileName FN( FileName );
    FN.MakeRelativeTo( m_GUI->GetProjectPath() );
    wxString Ext = FN.GetExt();
    FileName = FN.GetFullPath();
    if( Ext == _T( "zip" ) || Ext == _T( "ZIP" ) ) {
      FileName.Append( _T( "#zip:*.xrc" ) );
    }
    wxString CurrentContent = AutoLoad->GetValue();
    if( CurrentContent.Length() && ( CurrentContent[CurrentContent.Length() - 1] != _T( '\n' ) ) ) {
      AutoLoad->AppendText( _T( "\n" ) );
    }
    AutoLoad->AppendText( FileName );
    AutoLoad->AppendText( _T( "\n" ) );
  }
}

void wxWidgetsGUIConfigPanel::OnButton2Click( M_Code_unused wxCommandEvent & event ) {
  AutoLoad->Clear();
}

wxString wxWidgetsGUIConfigPanel::GetTitle() const {
  return _T( "wxSmith" );
}

wxString wxWidgetsGUIConfigPanel::GetBitmapBaseName() const {
  return wxEmptyString;
}

void wxWidgetsGUIConfigPanel::OnApply() {
  wxStringTokenizer Tokens( AutoLoad->GetValue(), _T( "\n" ) );
  m_GUI->m_LoadedResources.Clear();
  while( Tokens.HasMoreTokens() ) {
    m_GUI->m_LoadedResources.Add( Tokens.GetNextToken() );
  }
  m_GUI->m_MainResource = MainRes->GetValue();
  m_GUI->m_CallInitAll = InitAll->GetValue();
  m_GUI->m_CallInitAllNecessary = m_GUI->m_CallInitAll && InitAllNecessary->GetValue();
  m_GUI->NotifyChange();
  m_GUI->OnRebuildApplicationCode();
}

void wxWidgetsGUIConfigPanel::OnCancel() {
}
